package cn.yunhe.java.service;

import cn.yunhe.java.dao.SysUserMapper;
import cn.yunhe.java.object.TableData;
import cn.yunhe.java.pojo.SysRoleUser;
import cn.yunhe.java.pojo.SysRoles;
import cn.yunhe.java.pojo.SysUser;
import cn.yunhe.java.utils.DateUtils;
import cn.yunhe.java.vo.UserRoleSetter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public TableData queryByCondition(String loginName, Integer userStatus,int page,int limit) {
        SysUser su = new SysUser();
        su.setLoginName(loginName);
        su.setUserStatus(userStatus);

        //分页
        PageHelper.startPage(page,limit);
        Page<SysUser> listPage = sysUserMapper.queryByCondition(su);
        //再次包装一次，比较麻烦 mapper接口直接返回Page<?>对象
        //PageInfo<SysUser> pageInfo = new PageInfo<>(suList);

        TableData td = new TableData();
        td.setCode(0);
        td.setMsg("OK");
        td.setCount(listPage.getTotal());
        td.setData(listPage.getResult());
        return td;
    }

    @Override
    public int updateUserStatus(Integer userId, Integer userStatus) {
        return sysUserMapper.updateUserStatus(userId,userStatus);
    }

    @Override
    public int saveUser(SysUser su) {
        return sysUserMapper.insertSelective(su);
    }

    @Override
    public int updateUser(SysUser su) {
        return sysUserMapper.updateByPrimaryKeySelective(su);
    }

    @Override
    public int delUser(Integer userId) {
        return sysUserMapper.deleteByPrimaryKey(userId);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public SysUser selectByPrimaryKey(Integer userId) {
        return sysUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public List<SysRoles> selectAllRoles(Integer userId) {
        return sysUserMapper.selectAllRoles(userId);
    }

    @Override
    public int doSetRole(UserRoleSetter urs,String userName) {
         //1.删除原有的角色信息
        sysUserMapper.deleteRoleByUserId(urs.getUserId());//操作一张表

         //2.将勾选的角色信息新增到用户角色对应表
        List<SysRoleUser> list = new ArrayList<>();
        for(String str : urs.getRoleIds()){
            SysRoleUser sru = new SysRoleUser();
            sru.setRoleId(Integer.parseInt(str));
            sru.setUserId(urs.getUserId());
            sru.setCreateUser(userName);
            sru.setCreateDate(DateUtils.getSystemDate());
            list.add(sru);
        }
        int rst = sysUserMapper.insertUserRoles(list);  //操作另外一张表
        return rst;
    }
}
